import React, { Component } from 'react';
import { Form, Row, Col } from 'antd';
import ShowType from '../../../routes/common/showType';
import styles from '../../../index.less';

const FormItem = Form.Item;

class TabAccidentComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: props.dataSet, /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sAccidentMemo: '', /* 问题描述 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const { masterConfig, sId } = this.state;
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sAccidentMemo: mainConfig.filter(item => (item.sName.toLowerCase() === 'saccidentmemo'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const { disabled, dataSet } = nextProps;
    this.setState({ disabled, dataList: dataSet });
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  render() {
    const {
      app, form, handleModalPane, dataName, dataType, onClose,
    } = this.props;
    const {
      dataList, sId, sActiveId, disabled, sAccidentMemo,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList,
      sId,
      disabled,
      handleModalPane,
      dataName,
      dataType,
      onClose,
    };
    const sAccidentMemoProps = { showConfig: sAccidentMemo, ...childProps };
    return (
      <FormItem className={styles.labelNo}>
        <Row type="flex">
          <Col
            span={24}
            order={sAccidentMemo.iOrder > 24 ? 24 : sAccidentMemo.iOrder}
            style={{ display: sAccidentMemo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sAccidentMemo" rows={6} textArea labelNo showDefaultName="问题描述" {...sAccidentMemoProps} />
          </Col>
        </Row>
      </FormItem>
    );
  }
}

export default TabAccidentComponent;
